import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        list: [],
        status:"all"
    },
    mutations: {
        // 添加案例
        setToDoList(state,list) {
            state.list=[...state.list,list]
        },
        // 改变小选框
        setIsDo(state,index) {
            state.list[index].isDo=!state.list[index].isDo
        },
        // 设置全大选框
        setAll(state,val) {
            state.list.map(item=>item.isDo=val)
        },
        // 删除list
        delSetList(state,ind) {
            state.list.splice(ind,1)
        },
        // 清除已完成
        clearList(state) { 
            state.list=state.list.filter(item=>!item.isDo)
        },
        // 设置任务栏
        setStatus(state, status) {
            state.status=status
        }
    },
    getters: {
        allList (state) {
          return state.list.every(item => item.isDo)
            
        },
        // 剩余数量
        surplusList(state) {
            return (state.list.filter(item=>!item.isDo)).length
        },
        isList(state) {
            if (state.status === 'no') {
              return  state.list.filter(item=>!item.isDo)
            } else if (state.status === 'yes') {
              return state.list.filter(item=>item.isDo)
            } else {
                return state.list
            }
        }
    },

    // 持久化
   plugins:[ createPersistedState({
       key: 'todolist',
       paths:['list']
})]
})